<?php namespace App\Http\Controllers\Api;

use App\Data;
use App\DataCardEducations;
use App\DataCardExtends;
use App\DataCardWorks;
use App\Http\Controllers\Controller;
use App\Http\Services\DoRequest;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Cache;
use Cache;

class DataController extends Controller
{
    public function getInfos(Request $request){
        $action = $request->get("action");

        if($action == "rebuild"){
            Cache::forget('page');
        }

        $page = 0;
        if(!Cache::has("page") || Cache::get("page") == "" || is_null(Cache::get("page"))){
            $page = 1;
            Cache::forever('page', $page);
        }else{
            $page = Cache::get('page');
            $page = (int)$page + 1;
            if($page > 300){
                return response()->json(["e"=>"complete"]);
            }
            Cache::put('page', $page, 1000);
        }

        if((int)$page > 0){
            $url1 = "https://v2s.qun.hk/v1/phonebook/members?id=09bb2e652d65&page=".$page;
            $header = [
                "authorization"=>"Bearer kVBaQ4xS3Xuarv17"
            ];

            $newRequestone = DoRequest::doGet($url1,$header);
            $listsCount = 0;
            if($newRequestone){
                $result = json_decode($newRequestone);

                if(isset($result->list)){
                    $lists = $result->list;
                    $listsCount = count($lists);
                    foreach ($lists as $list){
                        if(isset($list->card_id)){
                            $url2 = "https://v2s.qun.hk/v1/card/view?id=".$list->card_id."&phonebook_id=09bb2e652d65";
                            $header2 = [
                                "authorization"=>"Bearer kVBaQ4xS3Xuarv17"
                            ];

                            $newRequestone2 = DoRequest::doGet($url2,$header);
                            if($newRequestone2){
                                $result2 = json_decode($newRequestone2);

                                $hasData = Data::whereUserId($result2->user_id)->first();
                                if(!$hasData){
                                    $newData = new Data();
                                    $newData->card_id = $result2->card_id;
                                    $newData->user_id = $result2->user_id;
                                    $newData->realname = $result2->realname;
                                    $newData->pinyin = $result2->pinyin;
                                    $newData->avatar = $result2->avatar;
                                    $newData->sex = $result2->sex;
                                    $newData->phone = $result2->phone;
                                    $newData->email = $result2->email;
                                    $newData->title = $result2->title;
                                    $newData->company = $result2->company;
                                    $newData->position = $result2->position;
                                    $newData->birthday = $result2->birthday;
                                    $newData->address = $result2->address;
                                    $newData->intro = $result2->intro;
                                    $newData->citycode = $result2->citycode;
                                    $newData->cityname = $result2->cityname;
                                    $newData->save();

                                    if(isset($result2->cardEducations)){
                                        $cardEducations = $result2->cardEducations;
                                        if(count($cardEducations)>0){
                                            foreach ($cardEducations as $cardEducation){
                                                $newCardEducation = new DataCardEducations();
                                                $newCardEducation->university = $cardEducation->university;
                                                $newCardEducation->major = $cardEducation->major;
                                                $newCardEducation->from_date = $cardEducation->from_date;
                                                $newCardEducation->to_date = $cardEducation->to_date;
                                                $newCardEducation->description = $cardEducation->description;
                                                $newCardEducation->data_id    = $newData->id;
                                                $newCardEducation->save();
                                            }
                                        }
                                    }

                                    if(isset($result2->cardWorks)){
                                        $cardWorks = $result2->cardWorks;
                                        if(count($cardWorks)>0){
                                            foreach ($cardWorks as $cardWork){
                                                $newCardWorks = new DataCardWorks();
                                                $newCardWorks->company = $cardWork->company;
                                                $newCardWorks->position = $cardWork->position;
                                                $newCardWorks->from_date = $cardWork->from_date;
                                                $newCardWorks->to_date = $cardWork->to_date;
                                                $newCardWorks->description = $cardWork->description;
                                                $newCardWorks->data_id    = $newData->id;
                                                $newCardWorks->save();
                                            }
                                        }
                                    }

                                    if(isset($result2->cardExtends)){
                                        $cardExtends = $result2->cardExtends;
                                        if(count($cardExtends)>0){
                                            foreach ($cardExtends as $cardExtend){
                                                $newCardExtends = new DataCardExtends();
                                                $newCardExtends->key = $cardExtend->key;
                                                $newCardExtends->value = $cardExtend->value;
                                                $newCardExtends->data_id    = $newData->id;
                                                $newCardExtends->save();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }


        $allCount = Data::count();
        echo "complete".$allCount."|page:".$page;
    }
}